#include <iostream>
#include <pthread.h>
#include <unistd.h>

using namespace std;

__thread int cnt = 1;
//线程的局部存储

string addr(int &cnt)
{
    char buff[64];
    snprintf(buff, sizeof(buff), "%p", &cnt);
    return buff;
}

void*routine1(void *args)
{
    while(1)
    {
        cout << "thread-1 " << "[我来修改cnt], cnt:" << cnt 
            << ",&cnt:" << addr(cnt) << endl;
        sleep(1);
        cnt++;
    }
}

void*routine2(void *args)
{
    while(1)
    {
        cout << "thread-2 " << "cnt: " << cnt 
            << ",&cnt:" << addr(cnt) << endl;
        sleep(1);
    }
}

int main()
{
    pthread_t tid1, tid2;

    pthread_create(&tid1, nullptr, routine1, nullptr);
    pthread_create(&tid2, nullptr, routine2, nullptr);

    pthread_join(tid1, nullptr);
    pthread_join(tid2, nullptr);

    return 0;
}